package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.HeSuan;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.service.HeSuanService;
import com.example.demo.service.RoleService;
import com.example.demo.vo.DataView;
import com.example.demo.vo.HeSuanVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.List;

/**
 * @author Coding路人王
 * @date 2022/6/11 9:44
 */
@Controller
@RequestMapping("/hesuan")
public class HeSuanController {

    @Autowired
    private HeSuanService heSuanService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/toHeSuan")
    public String toHeSuan(){
        return "hesuan/hesuan";
    }

    @RequestMapping("/loadAllHeSuan")
    @ResponseBody
    public DataView loadAllHeSuan(HeSuanVo heSuanVo){
        IPage<HeSuan> page = new Page<>(heSuanVo.getPage(),heSuanVo.getLimit());
        QueryWrapper<HeSuan> queryWrapper = new QueryWrapper();
        queryWrapper.like(heSuanVo.getName() != null,"name",heSuanVo.getName());
        heSuanService.page(page,queryWrapper);
        return new DataView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("/addHeSuan")
    @ResponseBody
    public DataView addHeSuan(HeSuan heSuan){
        heSuanService.save(heSuan);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("添加核酸检测成功！");
        return dataView;
    }

    @RequestMapping("/updateHeSuan")
    @ResponseBody
    public DataView updateHeSuan(HeSuan heSuan, HttpSession session){
        //根据角色判断是否能编辑
        // 1.取到
        User user = (User) session.getAttribute("user");
        Integer uid = user.getId();
        List<Integer> role = roleService.queryUserRoleById(uid);
        Integer integer = role.get(0);
        Role byId = roleService.getById(integer);
        String roleName = byId.getName();
        DataView dataView = new DataView();
        if (StringUtils.equals(roleName,"学生") ){
            dataView.setCode(100);
            dataView.setMsg(roleName+"角色不能进行编辑！");
            return dataView;
        }


        heSuanService.updateById(heSuan);
        dataView.setCode(200);
        dataView.setMsg("修改核酸检测成功！");
        return dataView;
    }

    @RequestMapping("/deleteHeSuan")
    @ResponseBody
    public DataView deleteHeSuan(Integer id, HttpSession session){
        //根据角色判断是否能编辑
        // 1.取到
        User user = (User) session.getAttribute("user");
        Integer uid = user.getId();
        List<Integer> role = roleService.queryUserRoleById(uid);
        Integer integer = role.get(0);
        Role byId = roleService.getById(integer);
        String roleName = byId.getName();
        DataView dataView = new DataView();
        if (StringUtils.equals(roleName,"学生") ){
            dataView.setCode(100);
            dataView.setMsg(roleName+"角色不能进行删除！");
            return dataView;
        }


        heSuanService.removeById(id);
        dataView.setCode(200);
        dataView.setMsg("删除核酸检测成功！");
        return dataView;
    }

}
